package com.lagou.goods.service.impl;

import com.lagou.common.pojo.Goods;
import com.lagou.goods.mapper.GoodsMapper;
import com.lagou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Goods getGoodsById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public Integer deleteGoodsById(Integer id) {
        return goodsMapper.deleteById(id);
    }

    @Override
    public Integer updateGoodsById(Goods goods) {
        if(goods.getId()==0){
            return goodsMapper.insert(goods);
        } else {
            return goodsMapper.updateById(goods);
        }
    }

    @Override
    public Map<List<Goods>, Integer> getAllGoods(String name, String type,
                                                 Double priceS, Double priceE,
                                                 Integer stockS, Integer stockE,
                                                 Integer status, Integer pageIndex,
                                                 Integer pageSize) {
        Integer offset = (pageIndex-1)*pageSize;
//        System.out.println("offset = " + offset);
        List<Goods> goodsList = goodsMapper.getAllGoods(name, type, priceS, priceE, stockS, stockE, status, offset, pageSize);
        Integer total = goodsMapper.getCountOfGoods(name, type, priceS, priceE, stockS, stockE, status);
//        System.out.println("goodList = " + goodsList);
        HashMap<List<Goods>, Integer> map = new HashMap<>();
        map.put(goodsList, total);
        return map;
    }

}
